<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { smsHandleApi } from '@/api/controller';
  import { Download } from '@/utils'
  import { ToastError, ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import Pagination from "@/components/Pagination/index.vue";
  import addform from './components/addform.vue'
  import smsinfo from './components/smsinfo.vue'
  const formRef = ref(),
        smsinfoRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page: 1,
    limit: 20,
  })
  const filtersStatus = (status:number = -1)=>{
    if(status == -1) return ''
    if(status == 0) return '待发送'
    if(status == 1) return '已发送'
    if(status == 2) return '发送失败'
    if(status == 3) return '已撤回'
  }
  const handelOpenForm = (row:any = {})=>{
    formRef.value.openDialog(row)
  }
  const handelsmsInfo = (row:any = {})=>{
    smsinfoRef.value.openDialog(row)
  }
  const handleRevoke = (row:any = {})=>{
    ElMessageBox.confirm('是否撤销本次群发短信发送?', '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      const loading = Toastloading();
      await smsHandleApi.statuschage({
        smsid:row.id,
      })
      loading.close();
      ToastSuccess('撤回成功!');
      getList();
    }).catch(() => { })
  }
  const handelDownload = async(row:any)=>{
    const { id:smsid=0 } = row
    if(!smsid) return ToastError('smsid error')
    const res = await smsHandleApi.downloadfail({smsid})
    const { data } = res
    Download(data)
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await smsHandleApi.getsmsList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.list = data
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <el-button type="primary" @click="handelOpenForm()">发送短信</el-button>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="120" prop="id" label="ID" />
      <el-table-column width="180" prop="create_time" label="提交时间" />
      <el-table-column width="120" prop="send_status" label="发送状态">
        <template #default="scope">
          {{ filtersStatus(scope.row.send_status)  }}
        </template>
      </el-table-column>
      <el-table-column width="120" prop="send_nums" label="发送条数" />
      <el-table-column min-width="200" show-overflow-tooltip prop="content" label="文案展示" />
      <el-table-column width="180" sortable prop="sendtime" label="发送时间" />
      <el-table-column min-width="180" sortable prop="note" label="备注" />
      <el-table-column fixed="right" width="220" align="center" label="操作">
        <template #default="scope">
          <template v-if="scope.row.send_status == 0">
            <el-button type="primary" @click="handleRevoke(scope.row)">撤回</el-button>
            <el-button type="primary" @click="handelOpenForm(scope.row)">修改</el-button>
          </template>
          <template v-else-if="scope.row.send_status != 3">
            <el-button type="primary" @click="handelsmsInfo(scope.row)">查看详情</el-button>
            <el-button type="primary" @click="handelDownload(scope.row)">失败下载</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <addform ref="formRef" @updateList="getList" />
      <smsinfo ref="smsinfoRef" />
  </div>
</template>